﻿using Main.DataBaseContext;
using Main.dto;
using Main.enums;
using Main.messageEvent;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using static Main.App;
namespace Main.ViewModels
{
    public class ToDoTaskViewModel : BindableBase
    {
        private readonly IDialogService dialogService;
        private readonly IEventAggregator eventAggregator;
        private readonly BusnessContext busnessContext;

        public ICommand OpenAddNewTaskDialog { get; }

        public ICommand ShowEditToDoTaskDialog { get; }

        public ICommand AddOrUpdateToDoTask { get; }

        public string TaskNameForSearching { get; set; }

        public List<ToDoTaskStatus> ToDoTaskStatusList { get; }

        public ToDoTask ToDoTask { get; set; }

        public ToDoTaskViewModel(IDialogService dialogService,IEventAggregator eventAggregator,BusnessContext busnessContext)
        {
            this.dialogService = dialogService;
            this.eventAggregator = eventAggregator;
            this.busnessContext = busnessContext;
            ShowEditToDoTaskDialog = new DelegateCommand<ToDoTask>(ShowEditToDoTaskDialogHandler);

            OpenAddNewTaskDialog = new DelegateCommand(() => {

                dialogService.ShowDialog(AddToDoTaskDialogName, result => {

                    if (result.Result == ButtonResult.OK) {
                        // 甚至不需要调用 ef 来更新数据就能实现 动态刷新
                        //eventAggregator.GetEvent<RefreshToDoTaskViewEvent>().Publish();
                    }
                });
            });

            /*OpenAddNewTaskDrawer = new DelegateCommand(() => {
            
                // 打开侧边栏抽屉
            });*/

            ToDoTaskStatusList = new List<ToDoTaskStatus>() {
                new ToDoTaskStatus(){
                    Status=1,
                    Detail="待办"
                },
                new ToDoTaskStatus(){
                    Status=2,
                    Detail = "已办"
                }
            };

            AddOrUpdateToDoTask = new DelegateCommand(AddOrUpdateToDoTaskHandler);

            ToDoTask = new ToDoTask();
        }

        private async void AddOrUpdateToDoTaskHandler()
        {
            if (string.IsNullOrEmpty(ToDoTask.TaskSummary)) {

                MessageBox.Show("请输入有效的摘要信息");
                return;
            }
            busnessContext.ToDoTasks.Add(ToDoTask);
            await busnessContext.SaveChangesAsync();

            eventAggregator.GetEvent<RefreshToDoTaskViewEvent>().Publish();
        }

        private void ShowEditToDoTaskDialogHandler(ToDoTask task)
        {
            DialogParameters dialogParameters = new DialogParameters();

            dialogParameters.Add("ExistToDoTask",task);

            dialogService.ShowDialog(AddToDoTaskDialogName,dialogParameters, result => {

                if (result.Result == ButtonResult.OK)
                {
                    // 甚至不需要调用 ef 来更新数据就能实现 动态刷新
                    //eventAggregator.GetEvent<RefreshToDoTaskViewEvent>().Publish();
                }
            });
        }
    }
}
